<template>
    <div class="ProfessionalInfo">

        <div class="nav">
            <el-input v-model="input" placeholder="请输入课程名" style="width: 250px; margin-top: 15px;" clearable
                @keyup.enter.native="handleQuery">
                <i slot="prefix" class="el-input__icon el-icon-search" style="cursor: pointer; height: 10px;"
                    @click="handleQuery"></i>
            </el-input>
            <el-button style=" margin-top: 10px; margin-right: 20px; margin-left: 20px;" type="primary"
                @click="addMemberHandle">
                + 添加课程
            </el-button>
        </div>
        <div class="tab">
            <el-table style=" width: 100%; padding-top: 15px;" :data="tableData" stripe class="tableBox" :height="550">

                <el-table-column align="center" type="index" width="50"></el-table-column>
                <el-table-column align="center" prop="jiaoshigonghao" label="教师工号" width="90"></el-table-column>
                <el-table-column align="center" prop="jiaoshixingming" label="教师姓名" width="90"></el-table-column>
                <el-table-column align="center" prop="kechengmingcheng" label="课程名称" width="100"></el-table-column>
                <el-table-column align="center" prop="kechengleixing" label="课程类型" width="120"></el-table-column>
                <el-table-column align="center" prop="kechengxiangqing" label="课程详情" width="200"></el-table-column>
                <el-table-column prop="photo" label="图片" width="120" align="center">
                    <template slot-scope="scope">
                        <el-image style="height: 40px; border:none;cursor: pointer; border-radius: 5px;"
                            :src="getImage(scope.row.photo)"
                            :preview-src-list="[`http://localhost:8086/common/download?name=${scope.row.photo}`]">
                        </el-image>
                    </template>
                </el-table-column>
                <el-table-column prop="price" label="价格" width="70" align="center"></el-table-column>

                <el-table-column label=" 操作" width="220" align="center">
                    <template slot-scope="scope">
                        <el-button type="success" size="mini" class="blueBug" @click="update(scope.row)">
                            修改
                        </el-button>
                        <el-button type="danger" size="mini" class="delBut non" @click="deleteHandle(scope.row.id)">
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- 分页栏 -->
            <el-pagination class="pageList" :page-sizes="[4, 8, 12, 16]" :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper" :total="counts" :current-page.sync="page"
                @size-change="handleSizeChange" @current-change="handleCurrentChange">
            </el-pagination>
        </div>






        <el-drawer :title=mytitle :visible.sync="dialog" direction="rtl" custom-class="demo-drawer" ref="drawer">
            <div class="demo-drawer__content">
                <el-form :model="form">
                    <el-form-item label="课程名称" style="text-align: center;" :label-width="formLabelWidth">
                        <el-input v-model="form.kechengmingcheng" autocomplete="off" maxlength="100"></el-input>
                    </el-form-item>

                    <el-form-item label="课程类型" style="text-align: center;" :label-width="formLabelWidth">
                        <el-select v-model="drawCourse.coursetype" placeholder="请选择活动区域" @change="getCourseType"
                            @focus="returnShow">
                            <el-option v-for="(item, index) in drawCourse" :key="index" :label="item.coursetype"
                                :value="item.coursetype">
                            </el-option>
                        </el-select>
                    </el-form-item>

                    <el-form-item label="课程图片" style="text-align: center;" :label-width="formLabelWidth">
                        <el-upload class="avatar-uploader" action="http://localhost:8086/common/upload"
                            :show-file-list="false" multiple="multiple" :on-success="handleAvatarSuccess"
                            :before-upload="beforeAvatarUpload" :disabled="disabled">
                            <img v-if="imageUrl" :src="imageUrl" class="avatar">
                            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                        </el-upload>
                    </el-form-item>

                    <el-form-item label="教师工号" style="text-align: center;" :label-width="formLabelWidth">
                        <el-input v-model="form.jiaoshigonghao" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="课程详情" style="text-align: center;" :label-width="formLabelWidth">
                        <el-input v-model="form.kechengxiangqing" autocomplete="off" type="textarea" rows="4"></el-input>
                    </el-form-item>

                    <el-form-item label="教师姓名" style="text-align: center;" :label-width="formLabelWidth"
                        v-if="mytitle === '修改课程'">
                        <el-input v-model="form.jiaoshixingming" autocomplete="off"></el-input>
                    </el-form-item>

                    <el-form-item label="价格" style="text-align: center;" :label-width="formLabelWidth">
                        <el-input v-model="form.price" autocomplete="off"></el-input>
                    </el-form-item>

                </el-form>
                <div class="demo-drawer__footer">
                    <el-button @click="cancelForm">取 消</el-button>
                    <el-button type="primary" @click="submit()">确 定</el-button>
                </div>
            </div>
        </el-drawer>
    </div>
</template>

<script >
export default {
    name: 'ProfessionalInfo',
    data() {
        return {
            dialog: false,
            mytitle: "",
            form: {
                id: "",
                // 课程名称
                kechengmingcheng: '',
                photo: "",
                jiaoshigonghao: "",
                jiaoshixingming: "",
                kechengxiangqing: "",
                price: "",
                status: "",
                kechengleixing: "",
            },
            disabled: false,
            imageUrl: "",
            drawCourse: [],
            formLabelWidth: '80px',
            input: "",
            //总数据条数
            counts: 0,
            //当前页
            page: 1,
            //每页条数
            pageSize: 4,
            // 表格数据
            tableData: [],
        }
    },
    mounted() {
        this.getTableData();
    },
    methods: {
        returnShow() {

        },
        handleQuery() {
            this.getTableData();
        },
        handleSizeChange(val) {
            this.pageSize = val;
            this.page = 1;
            this.getTableData();
        },
        handleCurrentChange(val) {
            this.page = val;
            this.getTableData();
        },
        // 发布状态
        statusHandle(value) {
            if (value.status === 0) {
                value.status = 1;
            } else {
                value.status = 0;
            }
            this.axios.post("/kechengxinxi/update", value)
                .then(res => {
                    if (res.data === "success") {
                        this.$message({
                            type: "success",
                            message: "修改成功",
                        })
                    } else {
                        this.$message({
                            type: "error",
                            message: "修改失败",
                        })
                    }
                    this.getTableData();
                })
        },
        //删除
        deleteHandle(val) {
            this.$confirm('此操作将永久删除该类型, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.axios.delete("/kechengxinxi/delete/" + val)
                    .then(res => {
                        if (res.data === "success") {
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                        }
                        else {
                            this.$message({
                                type: 'error',
                                message: '删除失败!'
                            });
                        }
                        this.getTableData();
                    })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        // 修改
        update(val) {
            this.dialog = true;
            this.getCourseTypeInfo()
            this.mytitle = "修改课程";
            this.form = val;
            let name = val.photo;
            this.imageUrl = `http://localhost:8086/common/download?name=${name}`;

        },
        submit() {
            this.$confirm('确定要提交表单吗？')
                .then(_ => {
                    if (this.mytitle === "添加课程") {
                        this.axios.post("/kechengxinxi/save", this.form)
                            .then(res => {
                                if (res.data === "success") {
                                    this.$message({
                                        type: 'success',
                                        message: '添加成功!'
                                    });
                                }
                            })
                        this.getTableData();
                        this.dialog = false
                    } else if (this.mytitle === "修改课程") {
                        this.axios.post("/kechengxinxi/update", this.form)
                            .then(res => {
                                if (res.data === "success") {
                                    this.$message({
                                        type: "success",
                                        message: "修改成功",
                                    })
                                } else {
                                    this.$message({
                                        type: "error",
                                        message: "修改失败",
                                    })
                                }
                            })
                    }
                    //清空表单
                    Object.keys(this.form).forEach((key) => (this.form[key] = ''))
                    this.getTableData();
                    this.dialog = false;
                })
                .catch(_ => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
        },
        getCourseTypeInfo() {
            this.axios.get("/coursetype/list")
                .then(res => {
                    this.drawCourse = res.data;
                })
        },
        // 获取表格数据
        getTableData() {
            const params = {
                params: {
                    page: this.page,
                    pageSize: this.pageSize,
                    name: this.input,
                }
            }
            this.axios.get("/teacher/teacherCourse/page", params)
                .then(res => {
                    this.tableData = res.data.records;
                    this.counts = res.data.total;
                })
        },
        getImage(image) {
            return `http://localhost:8086/common/download?name=${image}`
        },
        //点击取消后的操作handler
        cancelForm() {
            this.dialog = false;
            this.getCourseTypeInfo();
            this.$message({
                type: 'info',
                message: '已取消删除'
            });
        },
        handleAvatarSuccess(response, file, fileList) {
            this.imageUrl = `http://localhost:8086/common/download?name=${response}`
            this.form.photo = response
        },
        beforeAvatarUpload(file) {
            const isLt2M = file.size / 1024 / 1024 < 2;
            if (!isLt2M) {
                this.$message.error('上传头像图片大小不能超过 2MB!');
            }
            return isLt2M;
        },
        addMemberHandle() {
            this.dialog = true;
            this.mytitle = "添加课程";
            this.getCourseTypeInfo();

        },
        //获取数据类型
        getCourseType(value) {
            this.form.kechengleixing = value;
        }

    }
}

</script>

<style scoped>
.demo-drawer__content {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: center;


}

/* 上传 */
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.avatar-uploader .el-upload:hover {
    border-color: #409EFF;
}

.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
}

.avatar {
    width: 130px;
    height: 130px;
    display: block;
    border-radius: 4px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04)
}

.avatar-uploader {
    text-align: center;
}

.ProfessionalInfo {
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    background-image: linear-gradient(to right, #eea2a2 0%, #bbc1bf 19%, #57c6e1 42%, #b49fda 79%, #7ac5d8 100%);
}

.nav {
    width: 60%;
    height: 10%;
    margin-bottom: 20px;
    display: flex;
    flex-direction: row;
    justify-content: center;

    background-color: rgba(255, 255, 255, 0.25);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    border: 0.8px solid rgba(255, 255, 255, 0.18);
    box-shadow: rgba(142, 142, 142, 0.19) 0px 6px 15px 0px;
    -webkit-box-shadow: rgba(142, 142, 142, 0.19) 0px 6px 15px 0px;
    border-radius: 19px;
    -webkit-border-radius: 19px;
    color: rgba(255, 255, 255, 0.75);
}

.nav .el-button {
    width: 170px;
    height: 50px;
    font-size: 18px;
    background: #fff;
    border: none;
    border-radius: 50px;
    font-family: mmmm;
    font-weight: 500;
    color: #000;
    outline: none;
    cursor: pointer;
    transition: all 0.4s;
}

.nav .el-button:hover {
    background-color: rgba(29, 201, 160, 0.08);
    border-color: #1DC9A0;
    color: #1DC9A0;
    transform: translateY(-5px) scale(1.05);
}

.nav .el-button:active {
    background-color: transparent;
    border-color: #1DC9A0;
    color: #1DC9A0;
    transform: translateY(5px) scale(0.95);
}

.nav .el-button:disabled {
    background-color: rgba(255, 255, 255, 0.16);
    color: #8E8E93;
    border-color: #8E8E93;
}

.tab {
    width: 80%;
    height: 70%;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    background-color: rgba(255, 255, 255, 0.25);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    border: 0.8px solid rgba(255, 255, 255, 0.18);
    box-shadow: rgba(142, 142, 142, 0.19) 0px 6px 15px 0px;
    -webkit-box-shadow: rgba(142, 142, 142, 0.19) 0px 6px 15px 0px;
    border-radius: 19px;
    -webkit-border-radius: 19px;
    color: rgba(255, 255, 255, 0.75);

}

.tab>>>.el-table tr {
    background-color: transparent;
}

.tab>>>.el-table--striped .el-table__body tr.el-table__row--striped td.el-table__cell {
    background-color: transparent;
}

.tab>>>.el-table th.el-table__cell {
    background-color: transparent;
}

.tableBox {
    background-color: transparent;
}

.pageList {
    margin-top: 10px;
    text-align: center;
}

.pageList>>>.el-pager li {
    background-color: transparent;
}
</style>